package com.gingko.duominuo.ui.widget

import android.content.Context
import android.text.TextUtils
import android.util.AttributeSet
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.annotation.DrawableRes
import androidx.annotation.IntDef
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import com.gingko.duominuo.App
import com.gingko.duominuo.R
import com.gingko.duominuo.utils.isConnected

/**
 * Describe：
 * - 空视图布局
 *
 * @author bsx
 * CreateTime：2019/8/13
 */
class EmptyLayout : LinearLayout, View.OnClickListener {
    private var rlErrorLayout: ConstraintLayout? = null
    private var tvHint: TextView? = null
    private var llContent: LinearLayout? = null
    private var ivError: ImageView? = null
    private var tvBottomBtn: TextView? = null
    private val mContext: Context
    private var listener: OnClickListener? = null
    private var bottomBtnListener: OnClickListener? = null
    var errorType = 0
        private set

    @IntDef(LOAD_ERROR, NO_DATA)
    @kotlin.annotation.Retention(AnnotationRetention.SOURCE)
    private annotation class ErrorType {}

    constructor(context: Context) : super(context) {
        mContext = context
        init(null)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        mContext = context
        init(attrs)
    }

    private fun init(attrs: AttributeSet?) {
        val typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.EmptyLayout)
        val bgColor = typedArray.getDrawable(R.styleable.EmptyLayout_backgroundColor)
        if (bgColor != null) {
            background = bgColor
        } else {
            setBackgroundColor(ContextCompat.getColor(mContext, R.color.background))
        }
        val bottomBtnText = typedArray.getString(R.styleable.EmptyLayout_bottomText)
        if (!TextUtils.isEmpty(bottomBtnText)) {
            tvBottomBtn?.text = bottomBtnText
        }
        typedArray.recycle()
        val view = View.inflate(mContext, R.layout.layout_empty, this)
        rlErrorLayout = view.findViewById(R.id.rl_error_view)
        ivError = view.findViewById(R.id.iv_error)
        tvHint = view.findViewById(R.id.tv_error)
        llContent = view.findViewById(R.id.ll_content)
        tvBottomBtn = view.findViewById(R.id.tv_bottom_btn)
        tvBottomBtn?.setOnClickListener(this)
        llContent?.setOnClickListener(this)
    }

    override fun onClick(v: View) {
        val id = v.id
        if (id == R.id.ll_content && listener != null) {
            listener?.onClick(v)
        } else if (id == R.id.tv_bottom_btn && bottomBtnListener != null) {
            bottomBtnListener?.onClick(v)
        }
    }
    /**
     * 显示空页面
     *
     * @param errorType   错误类型
     * @param errorImgRes 错误展示图片
     * @param errorMsg    错误提示
     */
    /**
     * 显示空页面（默认图片与文字）
     *
     * @param errorType 错误类型
     */
    @JvmOverloads
    fun show(@ErrorType errorType: Int, @DrawableRes errorImgRes: Int = 0, errorMsg: String? = null) {
        llContent?.visibility = View.VISIBLE
        this.errorType = errorType
        when (errorType) {
            LOAD_ERROR -> {
                if (isConnected(App.instance)) {
                    tvHint?.setText(R.string.error_view_load_error_click_to_refresh)
                    ivError?.setBackgroundResource(R.mipmap.img_nodata)
                } else {
                    this.errorType = NO_NETWORK
                    tvHint?.setText(R.string.no_network)
                    ivError?.setBackgroundResource(R.mipmap.img_nodata)
                    this.visibility = View.VISIBLE
                    return
                }
            }
            NO_DATA -> {
                ivError?.setBackgroundResource(R.mipmap.img_nodata)
                tvHint?.setText(R.string.error_view_no_data)
            }
            else -> {
            }
        }
        if (errorImgRes != 0) {
            ivError?.setBackgroundResource(errorImgRes)
        }
        if (!TextUtils.isEmpty(errorMsg)) {
            tvHint?.text = errorMsg
        }
        this.visibility = View.VISIBLE
    }

    /**
     * 显示空页面（默认图片）
     *
     * @param errorType 错误类型
     * @param errorMsg  错误提示
     */
    fun show(@ErrorType errorType: Int, errorMsg: String?) {
        this.show(errorType, 0, errorMsg)
    }

    fun hide() {
        llContent?.visibility = View.GONE
        tvBottomBtn?.visibility = View.GONE
        this.visibility = View.GONE
    }

    /**
     * 显示底部按钮(可通过自定义属性<bottomText>设置按钮文字)
     *
     * @param textContent ann
    </bottomText> */
    fun showBottomBtn(textContent: String?) {
        tvBottomBtn?.text = textContent
        tvBottomBtn?.visibility = View.VISIBLE
    }

    /**
     * 显示底部按钮（可通过自定义属性<bottomText>设置按钮文字）
    </bottomText> */
    fun showBottomBtn() {
        tvBottomBtn?.visibility = View.VISIBLE
    }

    fun setOnLayoutClickListener(listener: OnClickListener?) {
        this.listener = listener
    }

    fun setBottomBtnListener(listener: OnClickListener?) {
        bottomBtnListener = listener
    }

    companion object {
        /**
         * 数据加载失败
         */
        const val LOAD_ERROR = 1

        /**
         * 没有网络
         */
        const val NO_NETWORK = 2

        /**
         * 没有数据
         */
        const val NO_DATA = 3
    }
}